import hall
import cPickle, util, pyrpc
from bsddb import db


class StarState:
    def __init__ (self):
        self.starst = db.DB()
        self.starst.open (hall.dbconf['STAR.ST'],
                          dbtype = db.DB_HASH, flags = db.DB_CREATE)

    def setStar (self, threadID):
        self.starst.put('%s' % threadID, '1')

    def clearStar (self, threadID):
        self.starst.delete('%s' % threadID)
        
    def getStar (self, threadID):
        if (self.starst.get('%s' % threadID)):
            return True
        else:
            return False

    def mycmp (self, a, b):
        return b - a

    def getList (self, start, num):
        ls = self.starst.keys()
        ret = list(map(lambda x: int(x), ls))
        ret.sort(self.mycmp)        
        return ret[start : start + num];

if __name__ == '__main__':
    util.setLogging('starst.log')    
    util.initSignal()

    starSt = StarState()
    server = pyrpc.rpc.RPCServer()
    server.bindFunction(starSt.setStar)
    server.bindFunction(starSt.clearStar)
    server.bindFunction(starSt.getStar)
    server.bindFunction(starSt.getList)

    server.run(hall.servconf['STARST'][1])

